import {Pipe, PipeTransform} from '@angular/core';
import {DocApi} from '../entity/doc-api';

@Pipe({
    name: 'dubboApiReference'
})
export class DubboApiReferencePipe implements PipeTransform {

    transform(api: DocApi, args?: any): any {
        return `\`\`\`java
/**
 * @org.apache.dubbo.config.annotation.DubboReference${this.getVersionDeplay(api)}
 * private ${this.getMethodDefined(api, false)};
 */

//简写
@DubboReference${this.getVersionDeplay(api)}
private ${this.getMethodDefined(api, true)};
\`\`\``;
    }

    private getVersionDeplay(api: DocApi): string {
        if (!api.version) {
            return '';
        }
        return `(version="${api.version}")`;
    }

    private getMethodDefined(api: DocApi, simple: boolean): string {
        let code = api.code;
        let index = code.lastIndexOf('.');
        code = code.substring(0, index);

        index = code.lastIndexOf('.');
        if (simple) {
            // 不显示包名
            return code.substring(index + 1) + ' ' + code.substring(index + 1, index + 2).toLowerCase() + code.substring(index + 2);
        } else {
            // 显示包名
            code += ' ' + code.substring(index + 1, index + 2).toLowerCase() + code.substring(index + 2);
        }
        return code;
    }

}
